package com.sctvcloud.zhaohua_sd.ui.adapter.holder;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.adapter.holder.BaseRecylerHolder;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.beans.NewsItem;
import com.sctvcloud.zhaohua_sd.ui.fragment.CityStateFragment;
import com.sctvcloud.zhaohua_sd.ui.utils.GlideUtil;
import com.sctvcloud.zhaohua_sd.ui.utils.SkipUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/17 0017.
 */

public class CityStaHomepageHolder extends BaseRecylerHolder<NewsItem> implements View.OnClickListener {
    @BindView(R.id.item_city_sta_homepage_img)
    protected CustomEXImageView img;
    @BindView(R.id.item_city_sta_homepage_title)
    protected CustomFontTextView title;
    @BindView(R.id.item_city_sta_homepage_time)
    protected CustomFontTextView time;
    @BindView(R.id.item_city_sta_homepage_lable)
    protected CustomFontTextView lable;
    @BindView(R.id.item_city_sta_homepage_type)
    protected ImageView type;
    @BindView(R.id.item_city_sta_homepage_play_length)
    protected CustomFontTextView playTime;

    private NewsItem newsItem;
    private Activity activity;
    private String titleName;

    public CityStaHomepageHolder(Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    @Override
    protected void initInject() {
        super.initInject();
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(NewsItem newsItem) {
        this.newsItem = newsItem;
        title.setText(!TextUtils.isEmpty(newsItem.getNewsTitle()) ? newsItem.getNewsTitle() : "");
        titleName = title.getText().toString();
        // time.setText(DateUtils.getDateDayString(DateUtils.getDataDate(newsItem),""));
        time.setText(!TextUtils.isEmpty(newsItem.getPubTime()) ? DateUtils.formatDate(DateUtils.getDataDate(newsItem), "MM-dd HH:mm") : "");
        if (!TextUtils.isEmpty(newsItem.getLable())) {
            lable.setVisibility(View.VISIBLE);
            lable.setText(newsItem.getLable());
        } else {
            if (!TextUtils.isEmpty(newsItem.getNewsKeyword())) {
                lable.setVisibility(View.VISIBLE);
                lable.setText(newsItem.getNewsKeyword());
            } else {
                lable.setVisibility(View.GONE);
            }
        }
        if (!TextUtils.isEmpty(newsItem.getNewsMediaLength())) {
            playTime.setVisibility(View.VISIBLE);
            playTime.setText(newsItem.getNewsMediaLength());
        } else {
            playTime.setVisibility(View.GONE);
        }
        if (newsItem.getNewsType() == 2) { //音频
            type.setImageResource(R.mipmap.icon_home_voice);
            type.setVisibility(View.VISIBLE);
        } else if (newsItem.getNewsType() == 3) {   //视频
            type.setImageResource(R.mipmap.icon_home_play);
            type.setVisibility(View.VISIBLE);
        } else {
            type.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(newsItem.getNewsImage())) {
            GlideUtil.getGlideWithSmall169Def(context, newsItem.getNewsImage()).into(img);
        } else {
            img.setImageResource(R.mipmap.icon_def_16_9_s);
        }
    }

    @Override
    public void onClick(View v) {
        ArrayList<Pair<String, String>> pairs = new ArrayList<>();
        pairs.add(new Pair<>("页面", "首页"));
        pairs.add(new Pair<>("栏目", "市州"));
        pairs.add(new Pair<>("位置", "专题名称"));
        ArrayList<Pair<String, String>> pairsMap = new ArrayList<>();
        pairsMap.addAll(pairs);
        pairsMap.add(new Pair<>("行为类型", "点击"));
        GridsumWebDissector.getInstance().trackEvent(activity, "", titleName, "", 200, pairsMap);
        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> p : pairs) {
            map.put(p.first, p.second);
        }
        SkipUtil.skipWithNewsItem(context, newsItem, map);
    }
}
